//
//  ViewController.m
//  NSURLSession封装
//
//  Created by 周成龙 on 16/10/31.
//  Copyright © 2016年 ZCL. All rights reserved.
//

#import "ViewController.h"

#import "ZSessionHeader.h"
#import "HTTPTool.h"

#import "NetwokeManager.h"

@interface ViewController () <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate>

@property (nonatomic, strong) NSMutableData *dataM;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
//    [[HTTPTool shareHTTPTool] postCopleteData:^(NSData *data, NSError *error) {
//        NSLog(@"-1-data:%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
//    }];
    
    
    NetwokeManager *manager1 = [[NetwokeManager alloc] init];
    //"action":"getShowIndex"
    NSDictionary *dict = @{@"mobile":@"17756234759"};
    [manager1 POSTMethodURL:@"http://112.124.3.211:8099/Ios/Login/sendphone" parameters:dict uploadPreogerss:nil complete:^(NSData *data, NSError *error) {
        if (!error) {
            //NSLog(@"\n-1-%@", [NSThread currentThread]);
            NSLog(@"\n-1-data:-%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
        }
    }];
    
    // http://test.activityplus.com.cn/index.php/server/activity_list
    
//    NetwokeManager *manager2 = [[NetwokeManager alloc] init];
//    [manager2 GetMethodURL:@"http://test.activityplus.com.cn/index.php/server/activity_list" parameters:nil uploadPreogerss:nil complete:^(NSData *data, NSError *error) {
//        if (!error) {
//            NSLog(@"\n-2-%@", [NSThread currentThread]);
////            NSLog(@"\n-2-data:-%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
//        }
//    }];
}

// get 请求
- (void)getRequest {
    
    // http://test.activityplus.com.cn/index.php/server/activity_list
    NSURL *url = [NSURL URLWithString:@"http://test.activityplus.com.cn/index.php/server/activity_list"];
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask *data = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        // 解析服务器返回的数据
        NSLog(@"\n--%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
        // 默认在子线程中解析数据
        NSLog(@"\n==%@", [NSThread currentThread]);
    }];
    [data resume];
}
// post 请求
- (void)postRequest {
    NSURL *url = [NSURL URLWithString:@"http://siyi.api.xt-cloud.com/show"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // 默认就是GET请求，要发POST请求的话，就必须更改请求方式
    request.HTTPMethod = @"POST";
    
    NSDictionary *dict = @{@"action":@"getShowIndex"};
    NSString *bodyString = [NSString JSONStringWithDiction:dict];
    request.HTTPBody = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"data=%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
        NSLog(@"res=%@", request);
        NSLog(@"error=%@", error);
    }];
    [task resume];
}
// NSURLSessionDataTask设置代理 请求
- (void)POSTSetDelegate {
    
    NSURL *url = [NSURL URLWithString:@"http://siyi.api.xt-cloud.com/show"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // 默认就是GET请求，要发POST请求的话，就必须更改请求方式
    request.HTTPMethod = @"POST";
    
    NSDictionary *dict = @{@"action":@"getShowIndex"};
    NSString *bodyString = [NSString JSONStringWithDiction:dict];
    request.HTTPBody = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request];
    [task resume];
}

#pragma mark - NSURLSessionDelegate

#pragma mark - NSURLSessionTaskDelegate
// 请求完成或者是失败的时候调用
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(nullable NSError *)error {
    //解析服务器返回数据
//    NSLog(@"-1-%@", [[NSString alloc] initWithData:self.dataM encoding:NSUTF8StringEncoding]);
}

#pragma mark - NSURLSessionDataDelegate
// 接收到服务器响应的时候调用
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
 completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
    
    //子线程中执行
    NSLog(@"当前线程 --> %@", [NSThread currentThread]);
    
    self.dataM = [NSMutableData data];
    //默认情况下不接收数据
    //必须告诉系统是否接收服务器返回的数据
    completionHandler(NSURLSessionResponseAllow);
}

// 接受到服务器返回数据的时候调用,可能被调用多次
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
    didReceiveData:(NSData *)data {
    //拼接服务器返回的数据
    [self.dataM appendData:data];
}

@end
